package com.aftersale.demo.service.impl;

import com.aftersale.demo.entity.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aftersale.demo.entity.Faq;
import com.aftersale.demo.service.FaqService;
import com.aftersale.demo.mapper.FaqMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 *
 */
@Service
public class FaqServiceImpl extends ServiceImpl<FaqMapper, Faq>
    implements FaqService{

    @Autowired
    FaqMapper faqMapper;

    @Override
    public Result getFaq(Integer pageNum, Integer pageSize) {
        Page<Faq> faqPage=page(new Page<>(pageNum,pageSize));
        return Result.success(faqPage);
    }

    @Override
    public Result selectFaq(Integer pageNum, Integer pageSize, String title) {
        QueryWrapper<Faq> faqQueryWrapper=new QueryWrapper<>();
        faqQueryWrapper.like("title",title);
        Page<Faq> faqPage = page(new Page<>(pageNum,pageSize),faqQueryWrapper);
        return Result.success(faqPage);
    }

    @Override
    public Result addFaq(Faq faq) {
        Boolean key=save(faq);
        if(key){
            return Result.success("添加成功");
        }else {
            return Result.error("401","添加失败");
        }
    }

    @Override
    public Result deleteFaq(int id) {
        if(Objects.isNull(getById(id))){
            return Result.error("401","FAQ不存在");
        }else{
            faqMapper.deleteById(id);
            return Result.success("删除成功");
        }
    }

    @Override
    public Result updateFaq(Faq faq) {
        if(Objects.isNull(getById(faq.getId()))){
            return Result.error("401","FAQ不存在");
        }
        Boolean key=updateById(faq);
        if(key){
            return Result.success("修改成功");
        }else{
            return Result.error("401","修改失败");
        }
    }
}




